//修复 path 无法引用http://页面的问题 begin
String.prototype.startWith = function(str) {
	if (str == null || str == "" || this.length == 0
			|| str.length > this.length)
		return false;
	if (this.substr(0, str.length) == str)
		return true;
	else
		return false;
	return true;
}

function initIFrame() {
    var $parent = $(".admin-iframe").parent();
    if ($parent.hasClass('layui-body')) {
        $parent.addClass('admin-iframe-body');
        return;
    }
    if ($parent.hasClass('layui-tab-item')) {
        $parent.css({'padding': '0', 'overflow-y': 'hidden'});
    } else {
        $parent.css({'width': '100%', 'height': '100%'});
    }
}

layui.define([ 'layer', 'laytpl', 'element', 'form', 'base'], function (exports) {
    var $ = layui.$
    ,layer = layui.layer
    ,laytpl = layui.laytpl
    ,element = layui.element
    ,form = layui.form
    ,base=layui.base;
    var index = {
        //渲染菜单
        initNav: function () {
            var reqUrl="/baseService/menu/initNav";
            base.fsAjax(reqUrl, {}, function (data) {
                var menus = data.data;
                $('.layui-layout-admin .layui-header').load('/html/header.html', function () {
                    // laytpl(topNav.innerHTML).render(menus, function (html) {
                    //     // $('#topNav').after(html);
                    //     index.bindMenuFrom();
                    // });
                    index.bindMenuFrom();
                    element.render('nav');
                });
           
            });
        },
        // 路由注册
        initRouter: function () {
            Q.reg('console', function () {
                var menuPath = '/html/admin/console.html';
                index.loadView('console', menuPath, '主页', 0);
            });
            //首页注册
            Q.init({
                index: 'console'
            });
        },
        // 使用递归循环注册
        regRouter: function (menus) {
            $.each(menus, function (i, data) {
                if (data.url && data.url.indexOf('#!') == 0) {
                    Q.reg(data.url.substring(2), function () {
                        //临时保存url
                        data.menuPath.startWith("http://") ?  admin.putTempData("params",data.menuPath) : null ;
                        var menuId = data.url.substring(2);
                        var menuPath = data.menuPath.startWith("http://") ? 'pages/tpl/iframe.html' : 'pages/' + data.menuPath;
                        index.loadView(menuId, menuPath, data.name, data.treeSide);
                    });
                }
                if (data.subMenus) {
                    index.regRouter(data.subMenus);
                }
            });
        },
        //菜单绑定单击事件
        bindMenuFrom: function(){
            $(".layui-layout-admin .layui-side ul li").on('click','a', function(){
                var path = $(this).attr("path");
                var url =  $(this).attr("href");
                var menuName = $(this).text();//菜单名称
                if(path ){
                    index.loadView(url, path, menuName, 1);
                }
            });
        },
        // 路由加载组件
        loadView: function (menuId, menuPath, menuName, treeSide) {
            var contentDom = '.layui-layout-admin .layui-body';
            $(contentDom).load(menuPath, function () {
                element.render('breadcrumb');
                form.render('select');
            });
        },
        // 页面元素绑定事件监听
        bindEvent: function () {
            //主页
            $('#main').click(function () {
                Q.go('main');
            });
            // 退出登录
            $('#btnLogout').click(function () {
                layer.confirm('确定退出登录？', function () {
             
                });
            });
            // 修改密码
            $('#setPsw').click(function () {
                admin.req('/SysService/user/checkBeforeUpdatePwd', {}, function (data) {
                    if(data.code==200){
                        if(data.data){
                            admin.popupRight('pages/tpl/password.html');
                        }else{
                            layer.msg('您最近修改过密码，禁止频繁修改密码！', {icon: 2});
                            return false;
                        }
                    }else{
                        layer.msg('修改密码失败！', {icon: 2});
                        return false;
                    }
                }, 'POST');
            });
        },
    };
    exports('index', index);
});
